package b3.widget.basic
{
	import flash.display.Sprite;
	import flash.events.*;
	import flash.text.*;
	
	public class NumberSelecter extends Sprite
	{
    	private var numBox:TextField;
    	private var min:int;
    	private var _max:int;
    	private var _value:int;
    	private var numChangeCallback:Function;
    	
		public function NumberSelecter(LeftArrowButton:Class, RightArrowButton:Class, v:int = 1, min:int = 1, max:int = 99, backgroundFlag:Boolean = true, boxWidth:int = 30)
		{
			super();
			this.min = min;
			this.max = max;
			_value = v;
			var gap:int = 10;
			//左侧箭头
			var leftArrow:BasicButton = new BasicButton(LeftArrowButton);
			leftArrow.addEventListener(MouseEvent.CLICK, onLeftArrowMouseClick);
			addChild(leftArrow);
			//数字输入框
			numBox = new TextField();
			numBox.defaultTextFormat = new TextFormat("微软雅黑", 15, 0x000000);
			numBox.restrict = "0-9";
			numBox.width = boxWidth;
			numBox.height = 22;
			numBox.x = leftArrow.width + gap;
			numBox.type = TextFieldType.INPUT;
			numBox.background = backgroundFlag;
			numBox.text = v.toString();
			numBox.addEventListener(Event.CHANGE, onTextChange);
			addChild(numBox);
			//右箭头
			var rightArrow:BasicButton = new BasicButton(RightArrowButton);
			rightArrow.x = numBox.x + numBox.width + gap;
			rightArrow.addEventListener(MouseEvent.CLICK, onRightArrowMouseClick);
			addChild(rightArrow);
		}
		
		public function setNumChangeCallback(callback:Function):void
		{
			this.numChangeCallback = callback;
		}
		
		public function set max(m:int):void
		{
			_max = m;
			if (_value > m)
				_value = m;
		}
		
		public function get max():int
		{
			return _max;
		}
		
		public function set value(v:int):void
		{
			if (v < min)
				_value = max;
			else if (v > max)
				_value = min;
			else
				_value = v;
			numBox.text = value.toString();
			if (numChangeCallback != null)
				numChangeCallback();
		}
		
		public function get value():int
		{
			return _value;
		}
		
		public function onTextChange(event:Event):void
		{
			value = parseInt(numBox.text);
		}
		
		private function onLeftArrowMouseClick(event:MouseEvent):void
        {
        	value = parseInt(numBox.text) - 1;
        }
		
		private function onRightArrowMouseClick(event:MouseEvent):void
        {
        	value = parseInt(numBox.text) + 1;
        }
	}
}